<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <title>YUI focus/blur synth Tests</title>

    <script src="../../../build/yui/yui-debug.js"></script>
    <script src="window-focus.js"></script>
</head>
<body class="yui3-skin-sam">

    <div id="container">
        <button id="button-1">Click Me!</button>
        <a id="anchor-1" href="http://www.yahoo.com">Click Me!</a>
        <input type="text" id="text-1">
    </div>

    <script>
        YUI({
            filter: 'raw',
            filters: { 'event': 'debug' }
        }).use('console', 'test', 'event', 'event-simulate', 'window-focus', function (Y) {

            var Assert = Y.Assert;

            new Y.Console({ /*useBrowserConsole: true*/ }).render();

            Y.Test.Runner.add(new Y.Test.Case({
                name: "Event Focus And Blur Test",

                _should: {
                    fail: {
                        //test_purge_focus: 2528244, // fixed
                        //test_purge_blur: 2528244   // fixed
                    }
                },

                tearDown: function () {
                    Y.one('#container').purge(true);
                },

                test_add_focus: function(){

                    var foo = false,
                        target,
                        boundEl,

                        onFocus = function(e) {

                            foo = true;
                            boundEl = this;
                            target = e.target;

                        };

                    Y.on('focus', onFocus, '#container');

                    Y.one('#button-1').focus();

                    if (Y.isWindowInFocus()) {
                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
                        Assert.areEqual(Y.one('#button-1'), target, "the target is the incorrect node, should be the actual focus target");
                        Assert.areEqual(Y.one('#container'), boundEl, "the default scope should be the bound element");

                        foo = false;
                        target = null;
                        boundEl = null;

                        Y.one('#text-1').focus();
                        Y.one('#button-1').focus();

                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
                        Assert.areEqual(Y.one('#button-1'), target, "the target is the incorrect node, should be the actual focus target");
                        Assert.areEqual(Y.one('#container'), boundEl, "the default scope should be the bound element");

                        Y.one('#button-1').blur();
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_remove_focus: function () {

                    var foo = false,

                        onFocus = function(e) {

                            foo = true;

                        };

                    var handle = Y.on('focus', onFocus, '#container');

                    if (Y.isWindowInFocus()) {
                        Y.one('#button-1').focus();

                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");

                        handle.detach();

                        Y.one('#button-1').blur();

                        foo = false;

                        Y.one('#button-1').focus();

                        Assert.isFalse(foo, "container should not pickup the focus event after listener is removed");

                        Y.one('#button-1').blur();
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_purge_focus: function () {

                    var foo = false,

                        onFocus = function(e) {

                            foo = true;

                        };

                    if (Y.isWindowInFocus()) {
                        Y.on('focus', onFocus, '#container');

                        Y.one('#button-1').focus();

                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");

                        Y.Event.purgeElement('#container', false, 'focus');

                        Y.one('#button-1').blur();

                        foo = false;

                        Y.one('#button-1').focus();

                        Assert.isFalse(foo, "container should not pickup the focus event after listener has been purged");
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_bubble_order: function () {
                    var from = [],
                        doc  = Y.one(Y.config.doc),
                        body = Y.one('body'),
                        container = Y.one('#container');

                    function onFocus(e) {
                        from.push(this);
                    }

                    if (Y.isWindowInFocus()) {
                        container.on('focus', onFocus);
                        doc.on('focus', onFocus);
                        body.on('focus', onFocus);

                        Y.one('#anchor-1').focus();

                        Y.ArrayAssert.itemsAreSame([container, body, doc], from, "Incorrect bubble order");
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_add_blur: function () {

                    var foo = false,
                        target,
                        boundEl,

                        onBlur = function(e) {

                            foo = true;
                            boundEl = this;
                            target = e.target;

                        };

                    if (Y.isWindowInFocus()) {
                        Y.on('blur', onBlur, '#container');

                        Y.one('#button-1').focus();
                        Y.one('#button-1').blur();

                        Assert.isTrue(foo, "simple blur fails, container should pickup the focus event");
                        Assert.areEqual(target, Y.one('#button-1'), "the target is the incorrect node, should be the actual blur target");
                        Assert.areEqual(boundEl, Y.one('#container'), "the default scope should be the bound element");

                        foo = false;
                        target = null;
                        boundEl = null;

                        Y.one('#button-1').focus();
                        Y.one('#text-1').focus();

                        Assert.isTrue(foo, "simple blur fails, container should pickup the focus event");
                        Assert.areEqual(target, Y.one('#button-1'), "the target is the incorrect node, should be the actual blur target");
                        Assert.areEqual(boundEl, Y.one('#container'), "the default scope should be the bound element");
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_remove_blur: function () {
                    var foo = false,
                        onBlur = function(e) {
                            foo = true;
                        };

                    if (Y.isWindowInFocus()) {
                        var handle = Y.on('blur', onBlur, '#container');

                        Y.one('#button-1').focus();
                        Y.one('#button-1').blur();

                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
                        foo = false;
                        handle.detach();
                        Y.one('#button-1').focus();
                        Y.one('#text-1').focus();
                        Assert.isFalse(foo, "container should not pickup the blur event after listener has been removed");
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                },

                test_purge_blur: function () {
                    var foo = false,
                        onBlur = function(e) {
                            foo = true;
                        };

                    if (Y.isWindowInFocus()) {
                        Y.on('blur', onBlur, '#container');

                        Y.one('#button-1').focus();
                        Y.one('#button-1').blur();
                        Assert.isTrue(foo, "simple focus fails, container should pickup the focus event");
                        foo = false;
                        Y.Event.purgeElement('#container', false, 'blur');
                        Y.one('#button-1').focus();
                        Y.one('#text-1').focus();
                        Assert.isFalse(foo, "container should not pickup the blur event after listener has been purged");
                    } else {
                        Y.log("Window is not focused.", "warn", "TestRunner");
                    }
                }

            }));

            Y.Test.Runner.run();

        });
    </script>
</body>
</html>
